package com.hf.wujian.utils;

import com.hf.wujian.annotation.Dispatch;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnnotationUtils {


    public static <T extends Annotation> Map getAnnotationClassByMap(List<Class> classes,Class<T> annotation,String proName){
        Map<Integer,Object> result = new HashMap<>();
        //遍历传入的所有类
        for (Class aClass : classes) {
            //判断传入的类是否有相应注解
            if (aClass.isAnnotationPresent(annotation)) {
                //有注解的话将注解实例化，然后调用
                T mannotation = (T) aClass.getAnnotation(annotation);
                try {
                    Method method = annotation.getMethod(proName);
                    Object property = method.invoke(mannotation);
                    if(property instanceof Integer){
                        result.put((Integer) property,aClass);
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
